class ServerDB:
    servers = list()
    def read_file(self, filename):
        fh = open(filename, "r")
        for line in fh.readlines():
            lineData = line.strip().split(":", 3)
            if len(lineData) == 3:
                self.servers.append(Server(lineData[0], lineData[1], lineData[2]))
        fh.close()

    def lookup_common_name(self, common_name):
        for server in self.servers:
            if server.get_common_name() == common_name:
                return server
        return None

    def lookup(self, hostname):
        for server in self.servers:
            if server.get_hostname() == hostname:
                return server
        return None
    def get_servers(self):
        return self.servers
    
    def add_server(self, hostname, common_name, default_user):
        self.servers.append(Server(common_name, hostname, default_user))
    
    def write_file(self, filename):
        fh = open(filename, "w")
        for server in self.servers:
            fh.write(server.get_common_name() + ":" + server.get_hostname() + ":" + server.get_default_user() + "\n")
    
        fh.close()
        
class Server:
    common_name = ''
    hostname = ''
    default_user = ''   
    
    def __init__(self, common_name, hostname, default_user):
        self.common_name = common_name
        self.hostname = hostname
        self.default_user = default_user

    def get_common_name(self):
        return self.common_name

    def get_hostname(self):
        return self.hostname

    def get_default_user(self):
        return self.default_user
